import { request } from '../../request/index'
import runtime from '../../lib/runtime/runtime'
Page({
  data: {
    menulist: [],//左侧菜单数据
    shoplist: [],//右侧商品数据
    currentindex: 0,//被点击的左侧菜单项的索引
    scrollTop: 0//滚动条位置
  },
  cates: [],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //判断本地存储中是否存入了数据
    const cates = wx.getStorageSync("cates");
    if (!cates) {
      //如果数据不存在，则请求数据
      this.getcatelist()
    } else {
      if (Date.now() - cates.time > 1000 * 10) {//如果当前时间减去存入时间大于1w毫秒，重新发送请求
        this.getcatelist()
      } else {
        this.cates.data = this.cates
      }
    }
  },
  async getcatelist() {
    /* request({ url: "categories" })
       .then(res => {
         
       })*/
    const res = await request({ url: "categories" })
    this.cates = res.data.message
    wx.setStorageSync("cates", { time: Date.now(), data: this.cates });

    //左侧菜单数据
    let menulist = this.cates.map(item => item.cat_name)
    let shoplist = this.cates[0].children
    this.setData({
      menulist,
      shoplist
    })

  },
  handleItemTap(e) {
    const { index } = e.currentTarget.dataset
    let shoplist = this.cates[index].children
    this.setData({
      currentindex: index,
      shoplist,
      scrollTop: 0
    })

  }

})